﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class MemberDAO
    {
        private static DataClasses1DataContext m_nhomMuaDataContext = new DataClasses1DataContext();
        public static bool insertMember(MEMBER mMember)
        {
            try
            {               
                m_nhomMuaDataContext.MEMBERs.InsertOnSubmit(mMember);
                m_nhomMuaDataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool updateMember(MEMBER mMember)
        {
            try
            {
                MEMBER member = m_nhomMuaDataContext.MEMBERs.Single(temp => temp.ACCOUNT.USERNAME == mMember.ACCOUNT.USERNAME);
                member.ACCOUNT.ADDRESS = mMember.ACCOUNT.ADDRESS;
                member.ACCOUNT.CMND = mMember.ACCOUNT.CMND;
                member.ACCOUNT.FULLNAME = mMember.ACCOUNT.FULLNAME;
                member.ACCOUNT.PASSWORD = mMember.ACCOUNT.PASSWORD;
                member.ACCOUNT.PHONE_NUMBER = mMember.ACCOUNT.PHONE_NUMBER;
                m_nhomMuaDataContext.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }

        public static MEMBER getMember(string user_login)
        {
            try
            {
                MEMBER member = m_nhomMuaDataContext.MEMBERs.Single(temp => (temp.ACCOUNT.USERNAME == user_login));
                return member;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static MEMBER getMember(int idAccount)
        {
            try
            {
               bool isExist= m_nhomMuaDataContext.MEMBERs.Any(mem => (mem.IDACCOUNT==idAccount) && (mem.ACCOUNT.VISIBLE == true));
               if (!isExist)
                   return null;
                MEMBER member = m_nhomMuaDataContext.MEMBERs.Single(temp => (temp.IDACCOUNT == idAccount));
                return member;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool DeleteMember(int IDMember)
        {
            try
            {
                 MEMBER member = m_nhomMuaDataContext.MEMBERs.Single(temp => temp.IDMEMBER == IDMember);
                 if (member != null)
                 {
                     member.ACCOUNT.VISIBLE = false;
                     m_nhomMuaDataContext.SubmitChanges();
                     return CommentDAO.DeleteByAccount(member.IDACCOUNT);                     
                 }

                 return false;
            }
            catch
            {
                return false;
            }

        }
        public static MEMBER GetMember(string UserName, string PassWord)
        {
            try
            {              
                MEMBER member = m_nhomMuaDataContext.MEMBERs.Single(mem => (mem.ACCOUNT.USERNAME.Trim() == UserName) && (mem.ACCOUNT.PASSWORD.Trim() == PassWord));
                return member;
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
        }

        public static MEMBER GetMember(int idMember)
        {
            try
            {
               MEMBER Member= m_nhomMuaDataContext.MEMBERs.Single(mem => mem.IDMEMBER == idMember);
               return Member;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int GetIDMax()
        {
            int maxID = m_nhomMuaDataContext.MEMBERs.Max(comment => comment.IDMEMBER);
            return maxID;
        }

        public static bool IsExisting(string UserName, string PassWord)
        {
            return m_nhomMuaDataContext.MEMBERs.Any(mem => (mem.ACCOUNT.USERNAME.Trim() == UserName) && (mem.ACCOUNT.PASSWORD.Trim() == PassWord)&&(mem.ACCOUNT.VISIBLE==true)&&(mem.MEMBER_STYLE==3));        
        }

        public static bool IsExisting(string UserName)
        {
            return m_nhomMuaDataContext.MEMBERs.Any(mem => (mem.ACCOUNT.USERNAME.Trim() == UserName) && (mem.ACCOUNT.VISIBLE == true) && (mem.MEMBER_STYLE == 3));
        }

        public static bool IsExisting(int IDMember)
        {
            return m_nhomMuaDataContext.MEMBERs.Any(mem => (mem.IDMEMBER == IDMember) && (mem.ACCOUNT.VISIBLE == true) && (mem.MEMBER_STYLE == 3));
        }
    }
}
